//~ #define SKIPIT
#include "euqt.h"

#include <QtDebug>
#include <QSharedDataPointer>

//below 4.4 probably should generate an error

//4,5beta1 does this but not sure can be completely trusted
#if QT_VERSION > 0x040400
//~ #warning You have Qt >= 4.4.0 OK!
#endif

//4,5beta1 does this
#if QT_VERSION >= 0x040500
//~ #warning You have Qt >= 4.5.0 OK!
#endif

//4,53?
#if QT_VERSION >= 0x040502
#warning You have Qt >= 4.5.2 OK!
#endif


//~ extern "C"
//~ QApplication *app = qobject_cast<QApplication *>(qApp);
//~ Q_UNUSED(app)  /**/

extern "C"
QApplication *qt_app()
{
    return qobject_cast<QApplication *>(qApp);
}
    
//~ QSharedDataPointer<char *> S = new char [4096]; /* getProps*/
//~ QSharedDataPointer<QString *> S = new QString(); /*& getProps*/
//~ s.sprintf("-QT%04x-", (QT_VERSION % 0xffff00) >> 8);
/* will probably switch to some heap manager
forced to 2000 in strlcpy checking for len and adding \0
*/
char * SgetProps = new char [4096];
char * SgetChildren = new char [4096];
char * SgetStyles = new char [4096];
char * SgetProp_struct = new char [4096];
char * SgetProp_string = new char [4096];
char * SOFN = new char [4096];
char * Sinvoke = new char [4096];


extern "C"
QApplication *qt_init(int argc, char **argv)
{
	if (QApplication::instance())
	{
		return qApp;
	}
 //~ qDebug() << "init" << QThread::currentThreadId();
#ifdef __MINGW32__
	QApplication *app = new QApplication(argc, argv);
#else
	QApplication *app = new QApplication(0, NULL);
#endif

#ifdef SKIPIT
	//possible this is causing a known thread error crash on win98 when you click close button?
	//maybe should be using delete later instead of quit?
	QObject::connect(app, SIGNAL(lastWindowClosed()), app, SLOT(quit()));
#endif

	//~ return qApp;
	return app;
}

extern "C"
QCoreApplication *qt_init_noGUI(int argc, char **argv)
{
	if (QCoreApplication::instance())
	{
		return qApp;
	}

#ifdef __MINGW32__
	QCoreApplication *app = new QApplication(argc, argv);
#else
	QCoreApplication *app = new QApplication(0, NULL);
#endif

#ifdef SKIPIT
	//possible this is causing a known thread error crash on win98 when you click close button?
	//maybe should be using delete later instead of quit?
	QObject::connect(app, SIGNAL(lastWindowClosed()), app, SLOT(quit()));
#endif

	//~ return qApp;
	return app;
}

/* **** makes objects in the ui avilable with findChild
 connects some of the obvious singnals and slots
 may not be able to wrap this w/o low level UITools hacking
 setupUi
*/

/*** makes objects in the ui avilable with findChild
 can make no automatic connections between signals and slots?
needs to handle errors in the ui better
*/

extern "C"
QWidget *qt_loadUi(const char *string, QWidget *parent)
{
	QUiLoader loader;
	QBuffer buf;
	buf.open(QBuffer::ReadWrite);
	buf.write(string);
	buf.seek(0);
	QWidget *w = loader.load(&buf, parent);
	buf.close();

	if (w)
	{
		//should check there is already a centralWidget?
		if (!w->inherits("QDialog") && !w->inherits("QMainWindow"))
		{
			qDebug() << qApp->tr("!QDialog !QMainWindow ");
			//setCentralWidget(w);	who owns this?
		}
#ifdef SKIPIT
		//let caller set their own default close, or not
		w->setAttribute(Qt::WA_DeleteOnClose, true);
#endif
	}
	else
	{
		//in debug mode the console will report a bad ui
		//but may want this while testing
#ifdef SKIPIT
		QMessageBox::warning(0, "Error in LoadUi  ", "not a valid ui");
#endif
	}

	return w;
}

//gcc complains if have default param in cpp but is ok in .h
extern "C"
bool qt_run(bool once, QApplication *app)  // =0=qApp
{
 //~ qDebug() << "run" << QThread::currentThreadId();
	if (once)
	{
		app->processEvents();
		return true;
	}
	else
		return app->exec();
}

extern "C"
void qt_destroy(QWidget *w)
{
	delete w;
}

extern "C"
QWidget *qt_parentWidget(QWidget *w)
{
	return w->parentWidget();
}


//You must call show() to make the widget visible again.

extern "C"
void qt_setParent(QWidget *w,  QWidget *parent)
{
	w->parentWidget();
}


extern "C"
void qt_show(QWidget *w)
{
	w->show();
}

#define propsetter(name, type)						 \
extern "C" bool qt_set_ ## name ## _prop(QWidget *w, const char *prop, type val) \
{ \
  const QMetaObject *mo = w->metaObject(); \
  int i = mo->indexOfProperty(prop); \
  if(i == -1) return 0; \
  else return mo->property(i).write(w, val); \
}

propsetter(string, const char *)
propsetter(bool, bool)
propsetter(int, int)
propsetter(float, double)
propsetter(char, char)

extern "C"
const char *qt_get_string_prop(QWidget *w, const char *prop)
{
	const QMetaObject *mo = w->metaObject();
	int i = mo->indexOfProperty(prop);
	//~ return mo->property(i).read(w).toString().toLatin1().data();
	strlcpy(SgetProp_string, static_cast<const char*>(mo->property(i).read(w).toString().toLatin1().data()), 2000);//shareddata
	//~ qDebug() << qApp->tr("in S %1").arg(S);
	return SgetProp_string;
}

extern "C"
bool qt_get_bool_prop(QWidget *w, const char *prop)
{
	const QMetaObject *mo = w->metaObject();
	int i = mo->indexOfProperty(prop);
	return mo->property(i).read(w).toBool();
}

extern "C"
int qt_get_int_prop(QWidget *w, const char *prop)
{
	const QMetaObject *mo = w->metaObject();
	int i = mo->indexOfProperty(prop);
	return mo->property(i).read(w).toInt(NULL);
}

//get float?


//should just set a type to what the property is then can have one statement
//ranher than all the if else'es

extern "C"
const char *qt_get_struct_prop(QWidget *w, const char *prop)
{
	const QMetaObject *mo = w->metaObject();
	int i = mo->indexOfProperty(prop);

	//assert can trip the can't quit from other thread problem
	Q_ASSERT(i > -1);

	//~ return (void*)(mo->property(i).read(w).toByteArray());
	//~ return mo->property(i).read(w).toByteArray();
	QString t = mo->property(i).typeName();
	QString t4 = t.length() > 3 ? t.left(3) : "";

	SgetProp_struct[0] = '\0';
	if (t4 == "QPo")
	{
		//~ return "is a QPoint";
		//~ return mo->property(i).read(w).toPoint().toString().toLatin1().data();
		if (t == "QPointF")
		{
			QPointF qp = mo->property(i).read(w).toPointF();
			//~ return qApp->tr("{%1,%2}").arg(qp.x()).arg(qp.y()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2}").arg(qp.x()).arg(qp.y()).toUtf8().data()), 2000);//shareddata
		}
		else
		{
			QPoint qp = mo->property(i).read(w).toPoint();
			//~ return qApp->tr("{%1,%2}").arg(qp.x()).arg(qp.y()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2}").arg(qp.x()).arg(qp.y()).toUtf8().data()), 2000);//shareddata
			//~ t += "{%d,%d}";
			//~ return (const char *)(t.arg(qp.x(), qp.y()).toLatin1().data());
		}
	}
	else if (t4 == "QSi")
	{
		if (t == "QSizeF")
		{
			QSizeF qp = mo->property(i).read(w).toSizeF();
			//~ return qApp->tr("{%1,%2}").arg(qp.width()).arg(qp.height()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2}").arg(qp.width()).arg(qp.height()).toUtf8().data()), 2000);//shareddata
		}
		else
		{
			QSize qp = mo->property(i).read(w).toSize();
			//~ return qApp->tr("{%1,%2}").arg(qp.width()).arg(qp.height()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2}").arg(qp.width()).arg(qp.height()).toUtf8().data()), 2000);//shareddata
		}
	}
	else if (t4 == "QRe")
	{
		if (t == "QRectF")
		{
			QRectF qp = mo->property(i).read(w).toRectF();
			//~ return qApp->tr("{%1,%2,%3,%4}").arg(qp.x()).arg(qp.y()).arg(qp.width()).arg(qp.height()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2,%3,%4}").arg(qp.x()).arg(qp.y()).arg(qp.width()).arg(qp.height()).toUtf8().data()), 2000);//shareddata
		}
		else
		{
			QRect qp = mo->property(i).read(w).toRect();
			//~ return qApp->tr("{%1,%2,%3,%4}").arg(qp.x()).arg(qp.y()).arg(qp.width()).arg(qp.height()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2,%3,%4}").arg(qp.x()).arg(qp.y()).arg(qp.width()).arg(qp.height()).toUtf8().data()), 2000);//shareddata
		}
	}
	else if (t4 == "QLi")
	{
		if (t == "QLineF")
		{
			QLineF qp = mo->property(i).read(w).toLineF();
			//~ return qApp->tr("{%1,%2,%3,%4}").arg(qp.x1()).arg(qp.y1()).arg(qp.x2()).arg(qp.y2()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2,%3,%4}").arg(qp.x1()).arg(qp.y1()).arg(qp.x2()).arg(qp.y2()).toUtf8().data()), 2000);//shareddata
		}
		else
		{
			QLine qp = mo->property(i).read(w).toLine();
			//~ return qApp->tr("{%1,%2,%3,%4}").arg(qp.x1()).arg(qp.y1()).arg(qp.x2()).arg(qp.y2()).toUtf8().data();
			strlcpy(SgetProp_struct, static_cast<const char*>(qApp->tr("{%1,%2,%3,%4}").arg(qp.x1()).arg(qp.y1()).arg(qp.x2()).arg(qp.y2()).toUtf8().data()), 2000);//shareddata
		}
	}
	else if (t4 == "QSt")
	{
		//~ return mo->property(i).toUtf8().data();
		//~ return mo->property(i).read(w).toString().toUtf8().data();
		strlcpy(SgetProp_struct, static_cast<const char*>(mo->property(i).read(w).toString().toUtf8().data()), 2000);//shareddata
	}
	else
	{
		//~ return (const char *)(mo->property(i).typeName());
		strlcpy(SgetProp_struct, mo->property(i).typeName(), 2000);//shareddata
	}
	//~ qDebug() << qApp->tr("in S %1").arg(S);
	return SgetProp_struct;
}


//wID is different than a handle  and platform specific? why do you need it?
//~ extern "C"
//~ QWidget *qt_find(QWidget *parent, Wid wid)
//~ {
//~ return parent->find<QWidget *>(wid);
//~ }


extern "C"
QWidget *qt_findChild(QWidget *parent, const char *name, const char *oftype)
{
	//~ qDebug() << qApp->tr("in findChild name = %1").arg(QString(name));
	if (QString(oftype) == "QAction")
	{
		//~ return parent->findChild<QAction *>(QString(name));
		//~ qDebug() << qApp->tr("in oftype QMenu found= %1").arg(parent->findChild<QMenu *>(QString(name)) >0 );
		return (QWidget *)(parent->findChild<QAction *>(QString(name)));
	        //compiles but crashes
		//~ return qobject_cast<QWidget *>(parent->findChild<QAction *>(QString(name)));
	}
	else if (QString(oftype) == "QFrame")
	{
		return (QWidget *)(parent->findChild<QFrame *>(QString(name)));
	}
	else if (QString(oftype) == "QVBoxLayout")
	{
		return (QWidget *)(parent->findChild<QVBoxLayout *>(QString(name)));
	}
	else if (QString(oftype) == "QSplitter")
	{
		return (QWidget *)(parent->findChild<QSplitter *>(QString(name)));
	}
	else if (QString(oftype) == "QMainWindow")
	{
		return (QWidget *)(parent->findChild<QMainWindow *>(QString(name)));
	}

	if (parent &&  !parent->findChild<QWidget *>(name) )
	{
	    qDebug() << qApp->tr("parent && !foundChild = %1").arg(name);
	 
	    //non existant child crashes it too, have to find a better way
	    const QMetaObject *mo = (parent->findChild<QObject *>(name))->metaObject();
	    qDebug() << qApp->tr("is QObject ...");
	    qDebug() << qApp->tr("type = %1\n").arg((!mo)? "Failed or NonExistant":mo->className());

	    return (QWidget *)NULL;
	}

	//will crash on non existant widget or possibly other objects
	return parent->findChild<QWidget *>(QString(name));
}



extern "C"
bool qt_connect(QWidget *w, const char *signal, const char *slot, int rid)
{
	SimpleReceiver *r = new SimpleReceiver(w, slot, rid);
	return QObject::connect(w, signal, r, slot);
}

//slightly problematic at r, signalmapper?
extern "C"
bool qt_disconnect(QWidget *w, const char *signal, const char *slot)
{
	return QObject::disconnect(w, signal, 0, slot);
}

extern "C"
void qt_setFocus(QWidget *widget)
{
	widget->setFocus(Qt::MouseFocusReason);
}


//**
//Returns a translated version of sourceText, or sourceText itself if there is no appropriate translated version.
//the euphoria wrap defaults disambiguation=0 n=-1
// using .toUtf8()
//~ QMessageBox::warning(0, tr("Error"),tr("Unable to open %1").arg(c.toString()));
//values is a sequence of strings only, arg()'s will be tacked onto tr up to 5
//cjeck if tr can handle arg("") w/o specifiers then can just send 5 all the time


extern "C"
const char *qt_tr(QApplication *qt, const char *s, char **values, const char * disambiguation, int n)
{
	int idx = 0;
	//~ char *value = (char **)values[idx];
	char *value = values[idx];
	//~ qDebug() << qApp->tr("*values: %1").arg(*values);
	//~ qDebug() << qApp->tr("**values: }%1{").arg(**values);

	while (values[idx] && idx < 1000)
	{
		idx += 1;
	}

	//~ qDebug() << qApp->tr("idx: %1").arg(idx);

	if (!idx)
	{
		return qt->tr(s, disambiguation, n).toUtf8().data();
	}
	else if (idx == 1)
	{
		return (qt->tr(s, disambiguation, n).arg(*values[1])).toUtf8().data();
	}
	else if (idx == 2)
	{
		return (qt->tr(s, disambiguation, n).arg(*values[1]).arg(*values[2])).toUtf8().data();
	}
	return qt->tr(s, disambiguation, n).toUtf8().data();
}

extern "C"
void qt_move(QWidget *widget, int x, int y)
{
	//~ widget->move(QPoint(x, y));
	widget->move(x, y);
}

extern "C"
void qt_resize(QWidget *widget, int width, int height)
{
	widget->resize(width, height);
}


extern "C"
void qt_setWindowIcon(QWidget *widget, const char * icon)
{
	QString img = QString(*icon);
	QIcon ico = (img.length() > 3 && img.right(4) == ".ico")
				?  QIcon(icon)
				: QIcon(QPixmap(icon));
	const QMetaObject *mo = widget->metaObject();

	//why is this crashing?
	//~ int i = mo->indexOfClassInfo("QWidget");
	QString kls = mo->className();


	if (kls == "QWidget" || kls == "QMainWindow" || kls == "QDialog")
	{
		((QWidget *)widget)->setWindowIcon(ico);
		return;
	}

	//~ const QMetaObject *mo = w->metaObject();
	//~ int i = mo->indexOfProperty(prop);

	//~ QString t = mo->property(i).typeName();
	//~ QString t4 = t.length() > 3 ? t.left(3) : "";
	//~ if (t4 == "QPo")

	if (kls == "QMenu")
	{
		((QMenu *)widget)->setIcon(ico);
	}
	else if (kls == "QAction")
	{
		((QAction *)widget)->setIcon(ico);
	}
	else if (kls == "QPushButton")
	{
		((QPushButton *)widget)->setIcon(ico);
	}
	else if (kls == "QCheckBox")
	{
		((QCheckBox *)widget)->setIcon(ico);
	}
	else if (kls == "QToolButton")
	{
		((QToolButton *)widget)->setIcon(ico);
	}
	else if (kls == "QRadioButton")
	{
		((QRadioButton *)widget)->setIcon(ico);
	}
	else
	{
		qDebug() << qApp->tr("setIconQ_ASSERTclassName(): %1").arg(kls);
		//comboitem treeitem and on and on itemIcon() setItemIcon
		//~ Q_ASSERT(kls == "unsupported type"); //hi!

	}
	//~ class QToolBar' has no member named 'setIcon'

}

//could be made smarter if no setWindowTitle use property title if exists
extern "C"
void qt_setWindowTitle(QWidget *widget, const char * t)
{
	widget->setWindowTitle(t);
}

//have to do the is_visible show thing in c vrs euphoria
//need to have the origional position and can't get that from getprop yet
extern "C"
void qt_setWindowFlag(QWidget *widget, int flag)
{
	bool visible = widget->isVisible();
	//from the setflags example
	QPoint pos = widget->pos();
	if (pos.x() < 0)  pos.setX(0);
	if (pos.y() < 0)  pos.setY(0);

	widget->setWindowFlags(widget->windowFlags() | (Qt::WindowFlags)flag);
	widget->move(pos);
	if (visible)
	{
		widget->show();
	}
}

extern "C"
void qt_resetWindowFlag(QWidget *widget, int flag)
{
	bool visible = widget->isVisible();
	//from the setflags example
	QPoint pos = widget->pos();
	if (pos.x() < 0)  pos.setX(0);
	if (pos.y() < 0)  pos.setY(0);

	widget->setWindowFlags(widget->windowFlags() & (Qt::WindowFlags)~flag);
	widget->move(pos);
	if (visible)
	{
		widget->show();
	}
}

extern "C"
void qt_setAttribute(QWidget *widget, int attr, bool setreset)
{
	widget->setAttribute((Qt::WidgetAttribute)attr, setreset);
}





extern "C"
void qt_setTabOrder(QWidget *widget, QWidget * first, QWidget * second)
{
	widget->setTabOrder(first, second);
}


extern "C"
bool qt_inherits(QWidget *widget, const char *aclass)
{
	return widget->inherits(aclass);
}



//return all properties are coma sperated string

extern "C"
const char *qt_getProps(QWidget *widget)
{
	QMetaProperty prop;
	const QMetaObject *mo = widget->metaObject();
	QString rprops = "";
	qDebug() << qApp->tr("in getProps %1").arg(mo->className());
	//is a foreach faster?
	//userProperty?isUser

	for (int i = 0; i < mo->propertyCount(); i++)
	{
		prop = mo->property(i);
		if (i )	//isUser     all are && prop.isReadable() isValud
		{
			rprops.append(QString(",") + QString(prop.name()));
			//~ rprops.append(QString("=") + QString(prop.read ()));typeName()
		}
		else
		{
			//~  objectName usually first
			rprops.append(prop.name());
		}
	}


	strlcpy(SgetProps, static_cast<const char*>(rprops.toUtf8().data()), 2000);//shareddata
	//~ qDebug() << qApp->tr("in S %1").arg(S);
	return SgetProps;
}


//need to get more of the problematic objects, whatever is not currently showing
extern "C"
const char *qt_getChildrenNames(QWidget *widget)
{
	QMetaProperty prop;
	QString rprops = "";

    //may add oprion to show no name and other type, connection etc
	foreach(QWidget *o, widget->findChildren<QWidget*>())
	{
		const QMetaObject *mo = o->metaObject();
		int i = mo->indexOfProperty("objectName");
		QString  objn = mo->property(i).read(o).toString();
	    if (objn.isEmpty())
	    {
		continue;
	    }
		rprops.append(QString(",{") + 
	              QString(!objn.isEmpty()? objn:"no") + 
	         QString(", ") +
	              QString(mo->className() )+ 
	         QString("}") );
	}
	// repeat for actions
	foreach(QAction *o,  widget->findChildren<QAction *>() )
	{
		//~ const QMetaObject *mo = (QWidget *)(o->metaObject());
		const QMetaObject *mo = o->metaObject();
		int i = mo->indexOfProperty("objectName");
		QString  objn = mo->property(i).read(o).toString();
	    
	    if (objn.isEmpty())
	    {
		continue;
	    }
		rprops.append(QString(",{") + 
	              objn + 
	         QString(", QAction}") );
	}
	
	strlcpy(SgetChildren, static_cast<const char*>(rprops.toUtf8().data()), 2000);//shareddata
	//~ qDebug() << qApp->tr("in S %1").arg(S);
	return SgetChildren;
}



extern "C"
void qt_addItems(QObject *widget, char **values, const char *oftype)
{
	int idx = 0;
	char *value = values[idx];
	const QMetaObject *mo = widget->metaObject();
	QString kls = mo->className();
	//~ QObject *i;

	//not right yet, -1 when combo
	//~ int hasInsertItem = mo->indexOfMethod("insertItem( int index, const QStringList &)");
	int hasInsertItem = mo->indexOfMethod("insertItem( int , const QString &)");

	//~ does QComboBox has an add item iter?
	//~ if(kls == "QTreeWidget")
	//~ {
	//~ qobject_cast<QTreeWidgetItem *>(i) = new QTreeWidgetItem();
	//~ }

	while (value != NULL)
	{
		qDebug() << qApp->tr("in addItems idx=%1 val=%2 kls=%3 hasI=%4").arg(idx).arg(value).arg(kls).arg(hasInsertItem);

		//~ if(hasInsertItem >0)  needs more work
		//need a meta object can use as a cast
	    //~ QMetaObject::normalizedType(" int    const  *");


		if (0)
		{
			//~ QMetaObject::normalizedType((const char *)kls).insertItem(idx, value);
			//~ QMetaObject::normalizedType(static_cast<const char*>(kls.toUtf8().data())).insertItem(idx, value);
			//~ ((QMetaObject::normalizedType(kls) *)widget)->insertItem(idx, value);
		}
		else if (kls == "QComboBox")
		{
			((QComboBox *)widget)->insertItem(idx, value);
			//~ qobject_cast<QComboBox *>(widget)->insertItem(idx, value);
		}
		else if (kls == "QListWidget")
		{
			((QListWidget *)widget)->insertItem(idx, value);
			//~ has a QListWidgetItem too
			//~ qobject_cast<QListWidget *>(widget)->insertItem(idx, value);
		}
		//~ else if(kls == "QTreeWidget")
		//~ {
		//~ qobject_cast<QTreeWidgetItem *>(i)->setText(idx, value);
		//~ }
		idx += 1;
		value = values[idx];
	}

	//~ if(kls == "QTreeWidget")
	//~ {
	//~ qobject_cast<QTreeWidget *>(widget)->addTopLevelItem(qobject_cast<QTreeWidgetItem *>(i));
	//~ }
	return;
}



extern "C"
bool qt_setStyle(QWidget *widget, const char *style)
{
	QStyle *s = QStyleFactory::create(style);
	if (!s) return 0;

	//~ widget->setStyle(QString(style));
	widget->setStyle(s);
	foreach(QWidget *o, widget->findChildren<QWidget*>())
	{
		o->setStyle(s);
	}
	return 1;
}


//potential if send invalid widget you set style on whole app by mistake!
extern "C"
bool qt_setStyleSheet(QWidget *widget, const char *style)
{
	if (!style) return 0;
#ifdef Q_WS_MAC
	//~ Label.remove("font-family: sans-serif;");
	//~ Label.replace("font-size:12pt;", "font-size:14pt;");
	//~ Label.replace("font-size:8pt;", "font-size:10pt;");
#endif
	if (!widget)
	{
		qApp->setStyleSheet(style);
	}
	else
	{
		widget->setStyleSheet(style);
	}
	return 1;
}


//~
//~ The valid keys can be retrieved using the keys() function.
extern "C"
const char *qt_getStyles()
{
	QStringList styles = QStyleFactory::keys();
	QString rstyles = "";
	//entry would be nice here to avoid the leading coma
	foreach(QString nstyles, styles)
	{
		rstyles += nstyles + QString(",");
	}

	//~ qDebug() << qApp->tr("in getStyles %1").arg(rstyles);

	//strip the leading coma. done better in getProps
	rstyles = (rstyles.length()) ? rstyles.left(rstyles.length() - 1) : rstyles;
	//~ return (char *)rstyles; cannot convert
	//~ return q_const<char *>( rstyles);

	//these macro casts are probably not thread safe
	//~ return static_cast<char*>(rstyles.toUtf8().data());
	strlcpy(SgetStyles, static_cast<const char*>(rstyles.toUtf8().data()), 2000);//shareddata
	//~ qDebug() << qApp->tr("in S %1").arg(S);
	return SgetStyles;
}






//what about ,muitiple selection?
extern "C"
const char *qt_get_open_filename(QWidget *parent, const char *cap, const char *dir, const char *filter, int opts)
{
	strlcpy(SOFN, QFileDialog::getOpenFileName(parent, cap, dir, filter, 0, (QFileDialog::Options) opts).toLatin1().data(), 2000);
	return SOFN;
}

//want to get the real filename, usefull in other apps but seems unavailable
extern "C"
int qt_FontDialog()
{
	bool ok;
	QFont fnt = QFontDialog::getFont(&ok, 0);
	//~ QFont f = 
	if (ok) {
	    //~ fnt.dumpObjectInfo();
	    //~ qDebug() << fnt.toString();

	}
 
	return 0;
}
extern "C"
int qt_ColorDialog()
{
	//~ QColor f = 
	QColorDialog::QColorDialog(0);
	return 0;
}

extern "C"
void qt_about(QWidget *parent, const char *title, const char *message)
{
	QMessageBox::about(parent, title, message);
}

extern "C"
void qt_about_qt(QWidget *parent, const char *title)
{
	QMessageBox::aboutQt(parent, title);
}

extern "C"
int qt_critical(QWidget *parent, const char *title, const char *message, int buttons, int defaultButton)
{
	return (int) QMessageBox::critical(parent, title, message, (QMessageBox::StandardButtons) buttons,
									   (QMessageBox::StandardButton) defaultButton);
}

extern "C"
int qt_information(QWidget *parent, const char *title, const char *message, int buttons, int defaultButton)
{
	return (int) QMessageBox::information(parent, title, message, (QMessageBox::StandardButtons) buttons,
										  (QMessageBox::StandardButton) defaultButton);
}

extern "C"
int qt_question(QWidget *parent, const char *title, const char *message, int buttons, int defaultButton)
{
	return (int) QMessageBox::question(parent, title, message, (QMessageBox::StandardButtons) buttons,
									   (QMessageBox::StandardButton) defaultButton);
}

extern "C"
int qt_warning(QWidget *parent, const char *title, const char *message, int buttons, int defaultButton)
{
	return (int) QMessageBox::warning(parent, title, message, (QMessageBox::StandardButtons) buttons,
									  (QMessageBox::StandardButton) defaultButton);
}


extern "C"
void qt_treewidget_set_header_text(QTreeWidget *widget, int column, const char *name)
{
	QTreeWidgetItem *i = widget->headerItem();
	i->setText(column, QString(name));
}

extern "C"
void qt_treewidget_add(QTreeWidget *widget, char **values)
{
	int idx = 0;
	char *value = values[idx];
	QTreeWidgetItem *i = new QTreeWidgetItem();

	while (value != NULL)
	{
		i->setText(idx, value);
		idx += 1;
		value = values[idx];
	}

	widget->addTopLevelItem(i);
}

//for lcd numbers
extern "C"
void qt_display(QWidget *widget, const char * t)
{
	    ((QLCDNumber *)widget)->display(QString(t));
}



